home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_091 / adlrun / rtdict.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  261 lines

  1. #include <stdio.h>
  2. #include "adltypes.h"
  3. #include "adlprog.h"
  4.  
  5. struct data {            /* Structure of the data in the dictionary */
  6.     int16
  7.     type, val, first;
  8. };
  9.  
  10. struct letter {
  11.     char
  12.     let_name;        /* Name of this letter    */
  13.     struct data
  14.     *let_val;        /* Value of this token    */
  15.     struct letter
  16.     *next,            /* Next letter on this level */
  17.     *child;            /* Next level */
  18. };
  19.  
  20.  
  21. static struct letter
  22.     *trie;            /* The dictionary */
  23.  
  24. char
  25.     *malloc();
  26.  
  27.     /***************************************************************\
  28.     *                                *
  29.     *    new_letter( let ) - allocate a new letter for the    *
  30.     *    trie, initializing the letter to let.            *
  31.     *                                *
  32.     \***************************************************************/
  33.  
  34. static struct letter *
  35. new_letter( let )
  36. char
  37.     let;
  38. {
  39.     struct letter
  40.     *temp;
  41.  
  42.     temp = (struct letter *)malloc( sizeof( struct letter ) );
  43.     if( temp == (struct letter *)0 )
  44.     error( 27 );            /* Out of memory */
  45.     temp->let_name = let;
  46.     temp->let_val = (struct data *)0;
  47.     temp->next = temp->child = (struct letter *)0;
  48.     return temp;
  49. }
  50.  
  51.  
  52. insertkey( name, type, val, first )
  53. char
  54.     *name;
  55. int16
  56.     type, val, first;
  57. {
  58.     struct letter
  59.     **t,
  60.     *temp;
  61.     int16
  62.     done = 0,
  63.     retval;
  64.  
  65.     t = ≜
  66.     while( !done ) {
  67.     temp = *t;
  68.  
  69.     /* Find the first letter in the name */
  70.     while( (temp != (struct letter *)0) && (temp->let_name != *name) )
  71.         temp = temp->next;
  72.  
  73.     if( temp == (struct letter *)0 ) {
  74.         /* This letter wasn't in the trie, so put it there. */
  75.         temp = new_letter( *name );
  76.         temp->next = *t;
  77.         *t = temp;
  78.     }
  79.  
  80.     if( name[ 1 ] == '\0' ) {
  81.         /* We are at the end of the string.  Insert it */
  82.         if( temp->let_val != (struct data *)0 )
  83.         /* This name already defined. */
  84.         retval =  0;
  85.         else {
  86.         temp->let_val = (struct data *)malloc( sizeof( struct data ) );
  87.         if( temp->let_val == (struct data *)0 )
  88.             error( 27 );        /* Out of memory */
  89.         temp->let_val->type = type;
  90.         temp->let_val->val = val;
  91.         temp->let_val->first = first;
  92.         retval = 1;
  93.         }
  94.         done = 1;
  95.     }
  96.     else {
  97.         /* We have more letters to go.  Recursively insert them. */
  98.         name++;
  99.         t = &temp->child;
  100.     }
  101.     }
  102.     return retval;
  103. }
  104.  
  105.  
  106. int16
  107. lookup( name, val )
  108. char
  109.     *name;
  110. int16
  111.     *val;
  112. {
  113.     int16
  114.     retval,
  115.     done = 0;
  116.     struct letter
  117.     *t;
  118.  
  119.     name[ LENGTH ] = '\0';        /* Trim the name */
  120.     t = trie;
  121.     while( !done ) {
  122.     /* Find the first letter in the name */
  123.     while( (t != (struct letter *)0) && (t->let_name != *name) )
  124.         t = t->next;
  125.  
  126.     if( t == (struct letter *)0 ) {
  127.         /* This letter wasn't in the trie.  Return failure. */
  128.         retval =  -1;
  129.         done = 1;
  130.     }
  131.     else if( name[ 1 ] == '\0' ) {
  132.         /* We are at the end of the string.  See if we have data. */
  133.         if( t->let_val != (struct data *)0 ) {
  134.         *val = t->let_val->val;
  135.         retval =  t->let_val->type;
  136.         done = 1;
  137.         }
  138.         else {
  139.         /* No data. Check for abbreviation. */
  140.         for( t = t->child; !done; t = t->child ) {
  141.             if( t == (struct letter *)0 ) {
  142.             /* We should never get here, but... */
  143.             retval = -2;
  144.             done = 1;
  145.             }
  146.             else if( t->next != (struct letter *)0 ) {
  147.             /* There are other strings beginning with s */
  148.             retval = -2;
  149.             done = 1;
  150.             }
  151.             else if( t->let_val != (struct data *)0 ) {
  152.             /* There is data.  See if there is more string. */
  153.             if( t->child != (struct letter *)0 ) {
  154.                 /* Whoops - this is a substring. */
  155.                 retval = -2;
  156.                 done = 1;
  157.             }
  158.             else {
  159.                 /* Aha! An abbreviation! */
  160.                 *val = t->let_val->val;
  161.                 retval = t->let_val->type;
  162.                 done = 1;
  163.             }
  164.             }
  165.         }
  166.         }
  167.     }
  168.     else {
  169.         /* We have more letters to go.  Recursively check them */
  170.         name++;
  171.         t = t->child;
  172.     }
  173.     }
  174.     return retval;
  175. }
  176.  
  177.  
  178. static
  179. search_dict( t, s, proc )
  180. struct letter
  181.     *t;
  182. char
  183.     *s;
  184. int
  185.     (*proc)();
  186. {
  187.     int16
  188.     len;
  189.  
  190.     len = strlen( s );
  191.     while( t != (struct letter *)0 ) {
  192.     s[ len ] = t->let_name;
  193.     s[ len + 1 ] = '\0';
  194.     if( t->let_val != (struct data *)0 )
  195.         if( (*proc)( s, t->let_val ) != 0 )
  196.         return 1;
  197.     if( search_dict( t->child, s, proc ) != 0 )
  198.         return 1;
  199.     t = t->next;
  200.     }
  201.     return 0;
  202. }
  203.  
  204.  
  205. static int16
  206.     find_type,
  207.     find_val;
  208.  
  209. static int
  210. finder( name, dat )
  211. char
  212.     *name;
  213. struct data
  214.     *dat;
  215. {
  216.     if( (dat->type == find_type) && (dat->val == find_val) && dat->first )
  217.     return 1;
  218.     else
  219.     return 0;
  220. }
  221.  
  222.  
  223. char *
  224. findone( type, val )
  225. int16
  226.     type,
  227.     val;
  228. {
  229.     static char
  230.     buff[ 80 ];
  231.  
  232.     find_type = type;    find_val = val;
  233.     buff[ 0 ] = 0;
  234.     if( !search_dict( trie, buff, finder ) )
  235.     buff[ 0 ] = 0;
  236.     return buff;
  237. }
  238.  
  239.  
  240. read_symtab( fd, dir )
  241. int
  242.     fd;
  243. struct adldir
  244.     *dir;
  245. {
  246.     int
  247.     i, j;
  248.     struct symbol
  249.     symb;
  250.  
  251.     lseek( fd, dir->ptr, 0 );
  252.     for( i = 0; i < NUMSYM; i++ ) {
  253.     read( fd, &symb, sizeof( struct symbol ) );
  254.     for( j = 0; j < strlen( symb.name ); j++ )
  255.         symb.name[ j ] ^= CODE_CHAR;
  256.     insertkey( symb.name, symb.type, symb.val, symb.first );
  257.     }
  258. }
  259.  
  260. /*** EOF rtdict.c ***/
  261.